/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aktor;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author WIN7
 */
public class Konsultan {

    private String nama_konsultan;
    private String id_konsultan;
    private String alamat;
    private String no_telp;

    /**
     * @return the nama
     */
    public String getNama_Konsultan() {
        return nama_konsultan;
    }

    /**
     * @param nama the nama to set
     */
    public void setNama_Konsultan(String nama_konsultan) {
        this.nama_konsultan = nama_konsultan;
    }

    /**
     * @return the id_konsultan
     */
    public String getId_konsultan() {
        return id_konsultan;
    }

    /**
     * @param id_konsultan the id_konsultan to set
     */
    public void setId_konsultan(String id_konsultan) {
        this.id_konsultan = id_konsultan;
    }

    /**
     * @return the alamat
     */
    public String getAlamat() {
        return alamat;
    }

    /**
     * @param alamat the alamat to set
     */
    public void setAlamat(String alamat) {
        this.alamat = alamat;
    }

    /**
     * @return the no_telp
     */
    public String getNo_telp() {
        return no_telp;
    }

    /**
     * @param no_telp the no_telp to set
     */
    public void setNo_telp(String no_telp) {
        this.no_telp = no_telp;
    }

    public void InsertKonsultan() {
        try {
            Connection conn = DataBaseConnection.getDBConnection();;
            Statement s = conn.createStatement();
            String sql = "insert into Konsultan values('" + id_konsultan + "', '" + nama_konsultan + "','" + alamat + "','" + no_telp + "')";
//              String sql = "insert into Konsultan values('" + nama_konsultan + "','" + alamat + "','" + no_telp + "')";
          
            ResultSet rs = s.executeQuery(sql);
            JOptionPane.showMessageDialog(null, "Selamat data paket berhasil disimpan");
            rs.close();
            s.close();
            conn.close();
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Maaf data paket gagal disimpan");
            Logger.getLogger(Konsultan.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public final String ID_Konsultan() {
        try {
            String sql = "select max(ID_KONSULTAN) from KONSULTAN";
            Connection con = DataBaseConnection.getDBConnection();
            java.sql.Statement sta = con.createStatement();
            ResultSet rs = sta.executeQuery(sql);

            rs.next();
            String result = rs.getString(1);
            if (result != null) {
                int no = Integer.parseInt(result);
                no++;
                sql = "" + no;
                while (sql.length() <= 3) {
                    sql = "0" + sql;
                }
            } else {
                sql = "0001";
            }
            return sql;
        } catch (SQLException ex) {
            Logger.getLogger(Konsultan.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }

    }

   
    public ArrayList<Konsultan> getNama() {
        ArrayList<Konsultan> ArrayNama = new ArrayList<Konsultan>();
        try {
            Connection conn = DataBaseConnection.getDBConnection();;
            Statement s = conn.createStatement();
            String sql = "select nama_konsultan, id_konsultan from konsultan";
            ResultSet rs = s.executeQuery(sql);
            while (rs.next()){
                Konsultan kon = new Konsultan();
                kon.setNama_Konsultan(rs.getString(1));
                kon.setId_konsultan(rs.getString(2));
                
                ArrayNama.add(kon);
            }
            
            rs.close();
            s.close();
        } catch (SQLException ex) {
            Logger.getLogger(Konsultan.class.getName()).log(Level.SEVERE, null, ex);
        }
        return ArrayNama;
    }
    public boolean LoginKonsultan(String username, String password)  {
        try {
            Connection conn = new DataBaseConnection().getDBConnection();
            Statement st = conn.createStatement();
            ResultSet rs = st.executeQuery("Select ID_konsultan,NAMA_konsultan from konsultan Where ID_konsultan = '" + password + "' and NAMA_konsultan ='" + username + "'");
            if (rs.next()) {
                this.id_konsultan = rs.getString(1);
                this.nama_konsultan = rs.getString(2);
                return true;

            }
        } catch (SQLException ex) {
            Logger.getLogger(Pasien.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }
}
